package com.qiankunxu.initializer;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : Hunter Huang
 * @since : 2023/12/13 20:45
 * {@literal : 用于环境属性注册}
 */
public class MyApplicationContextInitializer implements ApplicationContextInitializer {
    /**
     * IOC容器对象创建完毕后执行
     * 1. 准备属性
     * 2. 获取一个属性资源管理对象
     * 3. 注册
     *
     * @param applicationContext 注入环境属性
     */
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        Map<String, Object> myMap = new HashMap<>();
        myMap.put("applicationName", "big-event");
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        MutablePropertySources propertySources = environment.getPropertySources();
        propertySources.addLast(new MapPropertySource("myMap", myMap));

        //故意使用异常做测试用
//        int i = 1/0;

    }
}
